﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using AllTalk.AllTalkProxyService;
using System.ServiceModel;

namespace AllTalk
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            // Set the data context of the listbox control to the sample data
            DataContext = App.ViewModel;
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        // Load data for the ViewModel Items
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            //if (!App.ConnectionManager.IsConnected)
            //{
            //    App.ConnectionManager.Connect();
            //}
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            this.FriendsList.SelectedIndex = -1;
        }

        private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (this.FriendsList.SelectedIndex != -1)
            {
                string email = App.ViewModel.FriendViewModels[this.FriendsList.SelectedIndex].Email;
                NavigationService.Navigate(new Uri("/ChatPage.xaml?email=" + email, UriKind.RelativeOrAbsolute));
            }
        }

        private void FriendsList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.FriendsList.SelectedIndex != -1)
            {
                string email = App.ViewModel.FriendViewModels[this.FriendsList.SelectedIndex].Email;
                NavigationService.Navigate(new Uri("/ChatPage.xaml?email=" + email, UriKind.RelativeOrAbsolute));
            }
        }
    }
}